﻿/***************************************************************************
	Copyright (C) 2010 RapidWebDev Organization (Author: Eunge, Legal Name: Jian Liu, Email: eunge.liu@gmail.com)

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ***************************************************************************/

using System;
using System.Drawing;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using WebGridView = System.Web.UI.WebControls.GridView;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using BaoJianSoft.Common;

namespace BaoJianSoft.Web.Extensions.DynamicPages
{
	/// <summary>
	/// Checkbox field header template.
	/// </summary>
	public class CheckBoxFieldHeaderTemplate : ITemplate
	{
		/// <summary>
		/// When implemented by a class, defines the System.Web.UI.Control object that child controls and templates belong to. 
		/// These child controls are in turn defined within an inline template.
		/// </summary>
		/// <param name="container"></param>
		public void InstantiateIn(Control container)
		{
			CheckBox checkBox = new CheckBox() { ID = "CheckBoxFieldHeader" };
			checkBox.Attributes["onclick"] = "var checkboxSearcher = $('.GridViewCheckBoxFieldItem :checkbox'); checkboxSearcher.attr('checked', this.checked); checkboxSearcher.change()";
			container.Controls.Add(checkBox);
		}
	}

	/// <summary>
	/// Checkbox field items template.
	/// </summary>
	public class CheckBoxFieldItemTemplate : ITemplate
	{
		public const string CHECKBOX_COLUMN_HIDDENFIELD_ID = "CHECKBOX_COLUMN_HIDDENFIELD_ID";

		private string fieldName;
		private string bindingFormat;

		/// <summary>
		/// Construct Checkbox field items template.
		/// </summary>
		/// <param name="fieldName"></param>
		public CheckBoxFieldItemTemplate(string fieldName) : this(fieldName, null) { }

		/// <summary>
		/// Construct Checkbox field items template.
		/// </summary>
		/// <param name="fieldName"></param>
		/// <param name="bindingFormat"></param>
		public CheckBoxFieldItemTemplate(string fieldName, string bindingFormat)
		{
			this.fieldName = fieldName;
			this.bindingFormat = bindingFormat;
		}

		/// <summary>
		/// When implemented by a class, defines the System.Web.UI.Control object that child controls and templates belong to. 
		/// These child controls are in turn defined within an inline template.
		/// </summary>
		/// <param name="container"></param>
		public void InstantiateIn(Control container)
		{
			CheckBox checkBox = new CheckBox() { ID = "CheckBoxFieldItem", CssClass = "GridViewCheckBoxFieldItem" };
			HiddenField hiddenField = new HiddenField() { ID = CHECKBOX_COLUMN_HIDDENFIELD_ID };
			checkBox.DataBinding += new EventHandler(this.OnCheckBoxDataBinding);
			hiddenField.DataBinding += new EventHandler(this.OnHiddenFieldDataBinding);
			container.Controls.Add(checkBox);
			container.Controls.Add(hiddenField);
			(container as DataControlFieldCell).Attributes["AllowCellClickable"] = "false";
		}

		private void OnCheckBoxDataBinding(object sender, EventArgs e)
		{
			CheckBox checkbox = sender as CheckBox;
			try
			{
				GridViewRow gridViewRow = (GridViewRow)checkbox.NamingContainer;
				object fieldValue = this.bindingFormat == null ? DataBinder.Eval(gridViewRow.DataItem, this.fieldName) : DataBinder.Eval(gridViewRow.DataItem, this.fieldName, this.bindingFormat);
				checkbox.Visible = !string.IsNullOrEmpty(fieldValue == null ? null : fieldValue.ToString());
				checkbox.Attributes["PrimaryKeyFieldValue"] = fieldValue == null ? null : fieldValue.ToString();
			}
			catch (Exception exp)
			{
				Logger.Instance(this).Warn(exp);
				checkbox.Visible = false;
			}
		}

		private void OnHiddenFieldDataBinding(object sender, EventArgs e)
		{
			try
			{
				HiddenField hiddenField = sender as HiddenField;
				GridViewRow gridViewRow = (GridViewRow)hiddenField.NamingContainer;
				object fieldValue = this.bindingFormat == null ? DataBinder.Eval(gridViewRow.DataItem, this.fieldName) : DataBinder.Eval(gridViewRow.DataItem, this.fieldName, this.bindingFormat);
				hiddenField.Value = fieldValue == null ? null : fieldValue.ToString();
			}
			catch (Exception exp)
			{
				Logger.Instance(this).Warn(exp);
			}
		}
	}
}
